﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using System.Runtime.InteropServices;

namespace Server
{
    public partial class RemoteDesktop : Form
    {
        ScreenCapture obj;
        TcpChannel chan;
        String url;
        bool connected = false;
        private string ipAdress = null;

        public RemoteDesktop()
        {
            InitializeComponent();
        }
        public string IpAdress
        {
            get { return ipAdress; }
            set { ipAdress = value; }
        }
        [DllImport("user32.dll")]
        private static extern uint MapVirtualKey(
            uint cCode,
            uint uMapType
        );
        void start()
        {
            try
            {
                url = "Tcp://" + ipAdress + ":6600/MyCaptureScreenServer";
                chan = new TcpChannel();
                ChannelServices.RegisterChannel(chan,false);
                obj = (ScreenCapture)Activator.GetObject(typeof(ScreenCapture), url);
                connected = true;
                timer1.Enabled = true;
            }
            catch (Exception) { stop(); };
        }
        void stop()
        {
            try
            {
                timer1.Enabled = false;
                connected = false;
                ChannelServices.UnregisterChannel(chan);
            }
            catch (Exception) { }
        }

        private void bắtĐầuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            start();
        }

        private void dừngLạiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                url = "Tcp://" + ipAdress + ":6600/MyCaptureScreenServer";
                byte[] buffer = obj.GetDesktopBitmapBytes();
                MemoryStream ms = new MemoryStream(buffer);
                pictureBox1.Image = Image.FromStream(ms);
            }
            catch (Exception) { stop(); };
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (connected == true)
            {
                obj.MoveMouse(e.X, e.Y);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (connected == true)
            {
                obj.PressOrReleaseMouseButton(true, e.Button == MouseButtons.Left, e.X, e.Y);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (connected == true)
            {
                obj.PressOrReleaseMouseButton(false, e.Button == MouseButtons.Left, e.X, e.Y);
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (connected == true)
            {
                e.Handled = true;
                obj.SendKeystroke((byte)e.KeyCode, (byte)MapVirtualKey((uint)e.KeyCode, 0), true, false);
            }
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (connected == true)
            {
                e.Handled = true;
                obj.SendKeystroke((byte)e.KeyCode, (byte)MapVirtualKey((uint)e.KeyCode, 0), false, false);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            stop();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}